package edu;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class Add extends JFrame {
    private static final long serialVersionUID = -1928970409928880648L;
    JLabel jlpno = new JLabel("工      号：");
    JLabel jlname = new JLabel("姓      名：");
    JLabel jlsex = new JLabel("性      别：");
    JLabel jlnianling = new JLabel("年      龄：");

    JTextField jtpno = new JTextField("", 20);
    JTextField jtname = new JTextField("", 20);
    JTextField jtsex = new JTextField("", 20);
    JTextField jtnianling = new JTextField("", 20);

    JButton buttonadd = new JButton("添加");
    JButton buttonreturn = new JButton("返回");

    public Add() {
        JPanel jppno = new JPanel();
        JPanel jpname = new JPanel();
        JPanel jpsex = new JPanel();
        JPanel jpnianling = new JPanel();

        JPanel jpforbutton = new JPanel(new GridLayout(1, 1));

        jppno.add(jlpno);
        jppno.add(jtpno);

        jpname.add(jlname);
        jpname.add(jtname);

        jpsex.add(jlsex);
        jpsex.add(jtsex);

        jpnianling.add(jlnianling);
        jpnianling.add(jtnianling);

        jpforbutton.add(buttonadd);
        jpforbutton.add(buttonreturn);

        buttonadd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                boolean flag =false;
                flag= !jtpno.getText().equals("");

                System.out.println(flag);


                if ( !flag) {

                    if(e.getSource()==buttonadd){
                        javax.swing.JOptionPane.showMessageDialog(null, "未输入工号");
                    }

                }else {
                Connection conn = null;
                PreparedStatement ps = null;
                PreparedStatement ps1 = null;
                PreparedStatement ps2 = null;
                PreparedStatement ps3 = null;
                PreparedStatement ps4 = null;
                PreparedStatement ps5 = null;
                PreparedStatement ps6 = null;
                String xpno = jtpno.getText();
                String xname = jtname.getText();
                String xsex = jtsex.getText();
                String xnianling = jtnianling.getText();
                /*String xxiaoxue = jtxiaoxue.getText();
                String xchuzhong = jtchuzhong.getText();
                String xgaozhong = jtgaozhong.getText();
                String xdaxue =jtdaxue.getText();
                String xshuoshi =jtshuoshi.getText();
                String xboshi =jtboshi.getText();*/
                String sql = "INSERT INTO allstaff(pno,name,xingbie,age)" + "values(?,?,?,?)";
                String sql2 = "INSERT INTO xx(pno)" + "values(?)";
                String sql3 = "INSERT INTO cz(pno)" + "values(?)";
                String sql4 = "INSERT INTO gz(pno)" + "values(?)";
                String sql5 = "INSERT INTO dx(pno)" + "values(?)";
                String sql6 = "INSERT INTO ss(pno)" + "values(?)";
                String sql7 = "INSERT INTO bs(pno)" + "values(?)";
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    System.out.println("JBDC 加载成功!");
                } catch (Exception a) {
                    System.out.println("JBDC 狗带!");
                    a.printStackTrace();
                }
                try {
                    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/work", "root", "123456");
                    ps = conn.prepareStatement(sql);
                    ps1 = conn.prepareStatement(sql2);
                    ps2 = conn.prepareStatement(sql3);
                    ps3 = conn.prepareStatement(sql4);
                    ps4 = conn.prepareStatement(sql5);
                    ps5 = conn.prepareStatement(sql6);
                    ps6 = conn.prepareStatement(sql7);
                    ps1.setString(1, xpno);
                    ps2.setString(1, xpno);
                    ps3.setString(1, xpno);
                    ps4.setString(1, xpno);
                    ps5.setString(1, xpno);
                    ps6.setString(1, xpno);
                    ps.setString(1, xpno);
                    ps.setString(2, xname);
                    ps.setString(3, xsex);
                    ps.setString(4, xnianling);
                    /*ps.setString(3,xxiaoxue);
                    ps.setString(4,xchuzhong);
                    ps.setString(5,xgaozhong);
                    ps.setString(6,xdaxue);
                    ps.setString(7,xshuoshi);
                    ps.setString(8,xboshi);*/
                    ps.executeUpdate();
                    ps1.executeUpdate();
                    ps2.executeUpdate();
                    ps3.executeUpdate();
                    ps4.executeUpdate();
                    ps5.executeUpdate();
                    ps6.executeUpdate();
                } catch (SQLException b) {
                    System.out.println("1 ");
                    b.printStackTrace();
                } finally {
                    try {
                        conn.close();
                        System.out.println("MySQL 关闭成功");
                    } catch (SQLException c) {
                        System.out.println("MySQL 关闭失败 ");
                        c.printStackTrace();
                    }
                }
            }}
        });
        buttonreturn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        this.setTitle("添加员工信息");
        this.setLayout(new GridLayout(9, 1));
        this.add(jppno);
        this.add(jpname);
        this.add(jpsex);
        this.add(jpnianling);
        this.add(jpforbutton);
        this.setLocation(400, 300);
        this.setSize(350, 300);
        this.setVisible(true);
    }

}


